
local assets =
{
    Asset("ANIM", "anim/pocketwatch_weapon.zip"),
}

local prefabs = 
{
	"pocketwatch_weapon_fx",
}

local function GetStringsTable()
    return TUNING.NPC_CHAT_TABLE[ type(TUNING.__NPC_LANGUAGE)=="function" and TUNING.__NPC_LANGUAGE() or TUNING.__NPC_LANGUAGE ] ["npc_event_gift_from_wanda"] or {}
end


local function TryStartFx(inst, owner)
	owner = owner
			or inst.components.equippable:IsEquipped() and inst.components.inventoryitem.owner
			or nil

	if owner == nil then
		return
	end

	if inst.components.finiteuses:GetPercent() > 0 then
		if inst._vfx_fx_inst ~= nil and inst._vfx_fx_inst.entity:GetParent() ~= owner then
			inst._vfx_fx_inst:Remove()
			inst._vfx_fx_inst = nil
		end

		if inst._vfx_fx_inst == nil then
			inst._vfx_fx_inst = SpawnPrefab("pocketwatch_weapon_fx")
			inst._vfx_fx_inst.entity:AddFollower()
			inst._vfx_fx_inst.entity:SetParent(owner.entity)
			inst._vfx_fx_inst.Follower:FollowSymbol(owner.GUID, "swap_object", 15, 70, 0)
		end
	end
end

local function StopFx(inst)
    if inst._vfx_fx_inst ~= nil then
        inst._vfx_fx_inst:Remove()
        inst._vfx_fx_inst = nil
    end
end

-------------------------------------------------------------------------------
local function onequip(inst, owner)
    owner.AnimState:OverrideSymbol("swap_object", "pocketwatch_weapon", "swap_object")
    owner.AnimState:Show("ARM_carry")
    owner.AnimState:Hide("ARM_normal")

	TryStartFx(inst, owner)
end

local function onunequip(inst, owner)
    owner.AnimState:Hide("ARM_carry")
    owner.AnimState:Show("ARM_normal")

	StopFx(inst)
end

local function onequiptomodel(inst, owner, from_ground)
    StopFx(inst)
end

local function onattack(inst, attacker, target)
    if attacker == nil then
        return
    end
    if not attacker:HasTag("player") then
        return
    end

	if inst.components.finiteuses:GetPercent() > 0 then
		-- inst.components.fueled:DoDelta(-TUNING.TINY_FUEL)
        inst.SoundEmitter:PlaySound("wanda2/characters/wanda/watch/weapon/shadow_attack")		
	else
        inst.SoundEmitter:PlaySound("wanda2/characters/wanda/watch/weapon/attack")
	end
end

local function OnFuelChanged(inst, data)
    if data and data.percent then
        if data.percent > 0 then
            if not inst:HasTag("shadow_item") then
                inst:AddTag("shadow_item")
			    inst.components.weapon:SetDamage(TUNING.POCKETWATCH_SHADOW_DAMAGE)
				TryStartFx(inst)
            end
        else
            inst:RemoveTag("shadow_item")
		    inst.components.weapon:SetDamage(TUNING.POCKETWATCH_DEPLETED_DAMAGE)
			StopFx(inst)
        end
    end
end

local function CLIENT_PlayFuelSound(inst)
	local parent = inst.entity:GetParent()
	local container = parent ~= nil and (parent.replica.inventory or parent.replica.container) or nil
	if container ~= nil and container:IsOpenedBy(ThePlayer) then
		TheFocalPoint.SoundEmitter:PlaySound("dontstarve/common/nightmareAddFuel")
	end
end


local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

	-- inst:AddTag("weapon")
	inst:AddTag("pocketwatch")

    inst.AnimState:SetBank("pocketwatch_weapon")
    inst.AnimState:SetBuild("pocketwatch_weapon")
    inst.AnimState:PlayAnimation("idle", true)

    MakeInventoryFloatable(inst, "small", 0.05, {1.2, 0.75, 1.2})

	inst.playfuelsound = net_event(inst.GUID, "pocketwatch_weapon.playfuelsound")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
		--delayed because we don't want any old events
		inst:DoTaskInTime(0, inst.ListenForEvent, "pocketwatch_weapon.playfuelsound", CLIENT_PlayFuelSound)

        return inst
    end

    inst:AddComponent("npc_base_lib")
    inst:AddComponent("npc_everything_data")


    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem:ChangeImageName("pocketwatch_weapon")
	
    inst:AddComponent("inspectable")
    inst:AddComponent("named")
    inst.components.named:SetName(GetStringsTable().pocketwatch_name)
    STRINGS.CHARACTERS.GENERIC.DESCRIBE[string.upper("npc_item_pocketwatch_weapon")] = GetStringsTable().inspect_str  --人物检查的描述



    inst:AddComponent("lootdropper")	
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)
    inst.components.equippable:SetOnEquipToModel(onequiptomodel)
    inst.components.equippable.walkspeedmult = 2
	-- inst.components.equippable.restrictedtag = "pocketwatchcaster"

    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(TUNING.POCKETWATCH_SHADOW_DAMAGE*1.5)
    inst.components.weapon:SetRange(TUNING.WHIP_RANGE)
    inst.components.weapon:SetOnAttack(onattack)

    inst:AddComponent("finiteuses")
    inst.components.finiteuses:SetMaxUses(80)
    inst.components.finiteuses:SetUses(80)
    inst.components.finiteuses:SetOnFinished(function()
        local owner = inst.npc_base_lib:Get_Owner()
        if owner and owner:HasTag("player") then
            inst:PushEvent("unequipped",{owner = owner})
        end
        inst:Remove()
    end)


    inst:ListenForEvent("percentusedchange", OnFuelChanged)
	
    inst:DoTaskInTime(0, function() 
        OnFuelChanged(inst, { percent = inst.components.finiteuses:GetPercent() })
    end)

    MakeHauntableLaunch(inst)


    --------------------------------------------------------------------------------
    -- player.components.hunger.hungerrate
    inst:ListenForEvent("equipped",function(inst,_table)
        if _table == nil or _table.owner == nil or not _table.owner:HasTag("player") then
            return
        end
        local owner = _table.owner
        if owner.components.hunger then
            -- owner.components.hunger.hungerrate___old_for_pocketwatch = owner.components.hunger.hungerrate
            -- owner.components.hunger.hungerrate = TUNING.WILSON_HUNGER_RATE * 3
            owner.components.hunger.burnratemodifiers:SetModifier(inst,3)
        end
        -- owner.AnimState:SetErosionParams(0.05,-0.1,-1)
    end)
    inst:ListenForEvent("unequipped",function(inst,_table)
        if _table == nil or _table.owner == nil or not _table.owner:HasTag("player") then
            return
        end
        -- print("npc_item_pocketwatch_weapon  unequipped")
        local owner = _table.owner
        if owner.components.hunger then
            -- owner.components.hunger.hungerrate = owner.components.hunger.hungerrate___old_for_pocketwatch or TUNING.WILSON_HUNGER_RATE
            owner.components.hunger.burnratemodifiers:RemoveModifier(inst)
        end
        -- owner.AnimState:SetErosionParams(0,0,0)
        
    end)










    --------------------------------------------------------------------------------

    return inst
end

--------------------------------------------------------------------------------

return Prefab("npc_item_pocketwatch_weapon", fn, assets, prefabs)
